
import ObsClient from 'esdk-obs-browserjs/src/obs'

// OBS客户端实例
let obsClient = null

// 初始化OBS客户端
export const initObsClient = (config) => {
    obsClient = new ObsClient({
        access_key_id: config.accessKeyId,      // 你的AK
        secret_access_key: config.secretAccessKey, // 你的SK
        server: config.endPoint                 // 你的Endpoint
    })
    console.log(obsClient, "obsClient")
    return obsClient
}

// 获取OBS客户端实例
export const getObsClient = () => {
    if (!obsClient) {
        throw new Error('OBS客户端未初始化，请先调用initObsClient')
    }
    return obsClient
}

// 上传文件方法
export const uploadFile = async (key, file) => {
    return new Promise((resolve, reject) => {
        obsClient.putObject({
            Bucket: 'hzybucket',
            Key: key,
            SourceFile: file
        }, (err, result) => {
            if (err) {
                reject(err)
            } else {
                resolve(result)
            }
        })
    })
}

// 生成文件URL
export const getFileUrl = (bucketName, key, expires = 3600) => {
    return obsClient.createSignedUrlSync('getObject', {
        Bucket: bucketName,
        Key: key,
        Expires: expires
    })
}



// import { initObsClient, uploadFile } from '@/utils/obs'
// const accessKeyId = 'HKFQTM6CDZLRVAVNBB3F';
// const secretAccessKey = '20BVhH9GYwJIvOblBh8Fx6OjgttIyalpLSyWikse';
// const endPoint = 'obs.cn-south-1.myhuaweicloud.com';
// const bucketName = 'hzybucket';
//START obs直传----------------------------------
// initObsClient({
//   accessKeyId: accessKeyId,
//   secretAccessKey: secretAccessKey,
//   endPoint: endPoint
// });
// console.log(i, k, "文件")
// 生成文件路径
// const timestamp = new Date().getTime()
// const extension = k.name.split('.').pop()
// const fileName = `${timestamp}.${extension}`
// const fileKey = 'uploads/' + k.name
// const result = await uploadFile(fileKey, k)
// console.log(result, "上传结果")
// if (result.CommonMsg.Status < 300) {
//   console.log(`https://${bucketName}.${endPoint}/${fileKey}`)
// }
//END obs直传----------------------------------